package logic

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"goods_srv/goods"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderLogic {
	return &GetOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetOrderLogic) GetOrder(req *types.GetOrderRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	rey, err := l.svcCtx.GoodSrv.GetGoodsOf(l.ctx, &goods.GetGoodsOfRequest{GoodsId: req.UserId})
	if err != nil {
		return &types.Response{
			Code: -1,
			Msg:  "商品不存在",
			Data: nil,
		}, nil
	}
	if rey.GoodsId < rey.Status {
		return &types.Response{
			Code: -2,
			Msg:  "商品库存不充足",
			Data: nil,
		}, nil
	}
	amount := rey.Status * req.Amount
	fmt.Println(amount)
	stu := uuid.NewString()
	fmt.Println(stu)
	return &types.Response{
		Code: 0,
		Msg:  "订单创建成功",
		Data: map[string]interface{}{
			"rey": rey,
		},
	}, nil
}
